<?php
/**
 * 
 * 美容 处理类
 * @author tanzhe
 *
 */
class SalonModel {
	private $db;
	private $cache;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
	}
	
	public function getSalon($key, $shopId) { //获得美容院项目
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$salon = $this->cache->get('salon_'.$shopId);
		//$salon = array();
		if ( empty($salon) ) {
			$query = $this->db->query("select `type`,`label` from appearance where `shop_id`='{$shopId}' order by rank asc");
			$data = $this->db->fetchAllByQuery($query);
			$salon = array();
			foreach ( $data as $v ) {
				$salon[$v['type']][] = $v['label'];
			}
			ksort($salon);
			$this->cache->set('salon_'.$shopId, $salon);
		}
		return array('code'=>0,'ret'=>array('salon'=>$salon));
	}

	/**
	 * 
	 * 保存美容信息
	 * @param str $key
	 * @param int $type 1、男朋友 2、自己 3、好友
	 * @param str $style 发型
	 * @param str $color 颜色
	 * @param str $skin 皮肤
	 * @param str $friendId 好友美容预留
	 */
	function saveSalon($key, $type, $style, $color, $skin, $friendId=0) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( $type==1 ) {
			$sex = 1;
		} elseif ( $type==2 ) {
			$sex = 2;
		} else { //为朋友美容
			
		}
		$sex = $type==1 ? 1 : 2; 
		$appearance = array(
			'style'=>$playerInfo['style'],
			'color'=>$playerInfo['color'],
			'skin'=>$playerInfo['skin'],
		);
		if ( $type==1 ) { //为男朋友美容
			$boyfriend_id = $playerInfo['boyfriend_id'];
			if ( empty($boyfriend_id) ) {
				return array('code'=>204,'ret'=>'');
			}
			$boyfriendInfo = $this->cache->get('boyfriend_'.$boyfriend_id);
			if ( empty($boyfriendInfo) ) {
				return array('code'=>2,'ret'=>'');
			}
			$appearance = array(
				'style'=>$boyfriendInfo['style'],
				'color'=>$boyfriendInfo['color'],
				'skin'=>$boyfriendInfo['skin'],
			);
		}
		$needMoney = array();
		//换发型
		if ( !empty($style) ) {
			$styleData = $this->cache->get('salon_style_'.$style);
			if ( empty($styleData) ) {
				$query = $this->db->query("select * from appearance where `label`='{$style}'");
				$styleData = $this->db->fetchOneByQuery($query);
				$this->cache->set('salon_style_'.$style, $styleData);
			}
			if ( empty($styleData) OR ($styleData['sex'] != $sex )  ) { //不存在该发型
				return array('code'=>201,'ret'=>'');
			}
			if ( $appearance['style'] != $style ) { //新发型
				$needMoney['style'][$styleData['cost_type']] = $styleData['price']; 
			}
		}
		//换发型颜色
		if ( !empty($color) ) {
			$colorData = $this->cache->get('salon_color_'.$color);
			if ( empty($colorData) ) {
				$query = $this->db->query("select * from appearance where `label`='{$color}'");
				$colorData = $this->db->fetchOneByQuery($query);
				$this->cache->set('salon_color_'.$color, $colorData);
			}
			if ( empty($colorData) OR ($styleData['sex'] != $sex ) ) { //不存在该颜色
				return array('code'=>202,'ret'=>'');
			}
			if ( $appearance['color'] != $color ) { //新颜色
				$needMoney['color'][$colorData['cost_type']] = $colorData['price']; 
			}
		}
		//换皮肤
		if ( !empty($skin) && ($type == 2) ) {
			$skinData = $this->cache->get('salon_skin_'.$skin);
			if ( empty($skinData) ) {
				$query = $this->db->query("select * from appearance where `label`='{$skin}'");
				$skinData = $this->db->fetchOneByQuery($query);
				$this->cache->set('salon_skin_'.$skin, $skinData);
			}
			if ( empty($skinData) OR ($styleData['sex'] != $sex ) ) { //不存在该皮肤
				return array('code'=>202,'ret'=>'');
			}
			if ( $appearance['skin'] != $skin ) { //新皮肤
				$needMoney['skin'][$skinData['cost_type']] = $skinData['price']; 
			}
		}
		//未美容
		if ( count($needMoney) == 0 ) {
			return array('code'=>0,'ret'=>'');
		}
		//更新数据库和缓存
		$updateSalonData = array();
		$totalCoin = 0;
		$totalCash = 0;
		foreach ( $needMoney as $k=>$v ) {
			if ( isset($v[1]) ) {
				$totalCoin += $v[1];
			}
			if ( isset($v[2]) ) {
				$totalCash += $v[2];
			}
			$updateSalonData[$k] = $$k;
		}
		//判断游戏币是否足够
		if ( $totalCoin > $playerInfo['coin'] ) {
			return array('code'=>9,'ret'=>'');
		}
		//判断元宝是否足够
		if ( $totalCash > $playerInfo['cash'] ) {
			return array('code'=>10,'ret'=>'');
		}
		//更新数据库与缓存
		$updateData = array();
		$updateData['coin'] = $playerInfo['coin'] - $totalCoin;
		$updateData['cash'] = $playerInfo['cash'] - $totalCash;
		if ( $type==1 ) { //为男朋友美容
			$boyfriendInfo = array_merge($boyfriendInfo, $updateSalonData);
			//更新男朋友信息
			$options = array(
				'key'=>array(
					'id'=>$boyfriend_id,	
				),	
				'data'=>$updateSalonData,
			);
			$this->db->updateData('player_boyfriend', $options);
			$this->cache->set("boyfriend_".$boyfriend_id, $boyfriendInfo);
		} else {
			$playerInfo = array_merge($playerInfo, $updateSalonData);
			$updateData = array_merge($updateData, $updateSalonData);
		}
		$playerInfo = array_merge($playerInfo, $updateData);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( $totalCash > 0 ) {
			$aryData = array(
				'style'=>$style,
				'color'=>$color,
				'skin'=>$skin,
				'boyfriend'=>isset($boyfriend_id) ? $boyfriend_id : 0,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'salon',
				'num'=> -$totalCash,
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//返回个人信息
		$ret = array(
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
}
?>